package org.jeecg.modules.job.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.job.ums.entity.UmsCollect;
import org.jeecg.modules.job.ums.service.IUmsCollectService;
import org.jeecg.modules.job.wms.entity.*;
import org.jeecg.modules.job.wms.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/api/wms")
@Api(tags = "简历管理")
@CrossOrigin
public class WmsController {

    @Resource
    private IWmsResumeService resumeService;
    @Resource
    private IWmsJobExpectService jobExpectService;
    @Resource
    private IWmsProExpService proExpService;
    @Resource
    private IWmsWorkExpService workExpService;
    @Resource
    private IWmsEduExpService eduExpService;
    @Resource
    private IUmsCollectService collectService;
    @Resource
    private IWmsResumeFileService resumeFileService;
    @Resource
    private ISysBaseAPI sysBaseAPI;

    /**
     * 查询简历列表
     */
    @ResponseBody
    @GetMapping(value = "/getResumeList")
    @ApiOperation(value = "查询简历列表", notes = "查询简历列表")
    public Result<Object> getResumeList(WmsResume param,
                                        @RequestParam(name="page", defaultValue="1") Integer page,
                                        @RequestParam(name="limit", defaultValue="10") Integer limit) {
        IPage<WmsResume> pageList = resumeService.getResumePageList(new Page<WmsResume>(page, limit),param);
        return Result.OK(pageList);
    }

    /**
     * 查询简历基本信息
     */
    @ResponseBody
    @GetMapping(value = "/getResumeInfo")
    @ApiOperation(value = "查询简历基本信息", notes = "查询行业信息列表")
    public Result<Object> getResumeInfo() {
        try {
            //取得当前Token登录者会员的ID
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            String memberId = user.getId();
            WmsResume resume = resumeService.getOne(new QueryWrapper<>(new WmsResume().setMemberId(memberId)));
            if (resume == null) {
                return Result.error("请填写简历信息");
            }
            return Result.OK(resume);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询简历信息
     */
    @ResponseBody
    @GetMapping(value = "/getResume")
    @ApiOperation(value = "查询简历信息", notes = "查询简历信息")
    public Result<Object> getResume(WmsResume param) {
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            String memberId=user.getId();
            UmsCollect collectResult = null;
            //取得当前Token登录者会员的ID
            if (StringUtils.isEmpty(param.getId())) {
                param.setMemberId(memberId);
            } else {
                //非本人查询
                collectResult = collectService.getOne(new QueryWrapper<>(new UmsCollect().setMemberId(memberId)));
            }
            WmsResume resume = resumeService.getResume(param);
            //是否已读
            if (collectResult != null) {
                resume.setIfCollect(true);
            }
            return Result.OK(resume);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return Result.error("查询失败");
    }

    /**
     * 更新简历基本信息
     */
    @ResponseBody
    @RequestMapping(value = "/updateResumeBasic", method = RequestMethod.POST)
    @ApiOperation(value = "查询简历基本信息", notes = "查询行业信息列表")
    public Result<Object> updateResumeBasic(@RequestBody WmsResume resumeParam) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        resumeParam.setMemberId(user.getId());
        if (oConvertUtils.isEmpty(user.getPhone())){
            user.setPhone(resumeParam.getPhone());
            sysBaseAPI.updateUserInfo(user);
        }
        resumeService.updateResumeInfo(resumeParam);
        return Result.OK();
    }

    /**
     * 添加/更新期望职位
     */
    @ResponseBody
    @RequestMapping(value = "/updateJobExpect", method = RequestMethod.POST)
    @ApiOperation(value = "更新期望职位", notes = "更新期望职位")
    public Result<Object> updateJobExpect(@RequestBody WmsJobExpect jobExpectParam) {
        if (StringUtils.isEmpty(jobExpectParam.getId())){
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            jobExpectParam.setMemberId(user.getId());
        }
        jobExpectService.update(jobExpectParam);
        return Result.OK();
    }

    /**
     * 查询期望职位列表
     */
    @ResponseBody
    @RequestMapping(value = "/getJobExpectList", method = RequestMethod.GET)
    @ApiOperation(value = "查询期望职位", notes = "查询期望职位")
    public Result<Object> getJobExpectList() {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return Result.OK(jobExpectService.list(new QueryWrapper<>(new WmsJobExpect().setMemberId(user.getId()))));
    }


    /**
     * 查询期望职位详情
     */
    @ResponseBody
    @RequestMapping(value = "/getJobExpectDetail", method = RequestMethod.GET)
    @ApiOperation(value = "查询期望职位", notes = "查询期望职位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query")
    })
    public Result<Object> getJobExpectDetail(Long id) {
        return Result.OK(jobExpectService.getById(id));
    }


    /**
     * 根据ID删除期望职位
     */
    @ResponseBody
    @RequestMapping(value = "/deleteJobExpectById", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID删除期望职位", notes = "根据ID删除期望职位")
    public Result<Object> deleteJobExpectById(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.get("id").toString();
        if(StringUtils.isEmpty(id)){
            return Result.error("id不能为空");
        }
        return Result.ok(jobExpectService.removeById(id));
    }


    /**
     * 添加/更新工作经历
     */
    @ResponseBody
    @RequestMapping(value = "/updateWorkExp", method = RequestMethod.POST)
    @ApiOperation(value = "更新期望职位", notes = "更新期望职位")
    public Result<Object> updateWorkExp(@RequestBody WmsWorkExp workExpParam) {
        if (StringUtils.isEmpty(workExpParam.getId())){
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            workExpParam.setMemberId(user.getId());
        }
        workExpService.update(workExpParam);
        return Result.ok();
    }

    /**
     * 查询工作经历列表
     */
    @ResponseBody
    @RequestMapping(value = "/getWorkExpList", method = RequestMethod.GET)
    @ApiOperation(value = "查询工作经历列表", notes = "查询工作经历列表")
    public Result<Object> getWorkExpList() {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return Result.OK(workExpService.list(new QueryWrapper<>(new WmsWorkExp().setMemberId(user.getId()))));
    }


    /**
     * 查询工作经历详情
     */
    @ResponseBody
    @RequestMapping(value = "/getWorkExpDetail", method = RequestMethod.GET)
    @ApiOperation(value = "查询工作经历详情", notes = "查询工作经历详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query")
    })
    public Result<Object> getWorkExpDetail(Long id) {
        return Result.ok(workExpService.getById(id));
    }


    /**
     * 根据ID删除工作经历
     */
    @ResponseBody
    @RequestMapping(value = "/deleteWorkExpById", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID删除工作经历", notes = "根据ID删除工作经历")
    public Result<Object> deleteWorkExpById(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.get("id").toString();
        if(StringUtils.isEmpty(id)){
            return Result.error("id不能为空");
        }
        return Result.ok(workExpService.removeById(id));
    }


    /**
     * 添加/更新项目经历
     */
    @ResponseBody
    @RequestMapping(value = "/updateProExp", method = RequestMethod.POST)
    @ApiOperation(value = "添加/更新项目经历", notes = "添加/更新项目经历")
    public Result<Object> updateProExp(@RequestBody WmsProExp proExpParam) {
        if (StringUtils.isEmpty(proExpParam.getId())){
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            proExpParam.setMemberId(user.getId());
        }
        proExpService.update(proExpParam);
        return Result.ok();
    }

    /**
     * 查询项目经历列表
     */
    @ResponseBody
    @RequestMapping(value = "/getProExpList", method = RequestMethod.GET)
    @ApiOperation(value = "查询项目经历列表", notes = "查询项目经历列表")
    public Result<Object> getProExpList() {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return Result.OK(proExpService.list(new QueryWrapper<>(new WmsProExp().setMemberId(user.getId()))));
    }


    /**
     * 查询项目经历详情
     */
    @ResponseBody
    @RequestMapping(value = "/getProExpDetail", method = RequestMethod.GET)
    @ApiOperation(value = "查询项目经历详情", notes = "查询项目经历详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query")
    })
    public Result<Object> getProExpDetail(Long id) {
        return Result.ok(proExpService.getById(id));
    }

    /**
     * 根据ID删除项目经历
     */
    @ResponseBody
    @RequestMapping(value = "/deleteProExpById", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID删除项目经历", notes = "根据ID删除项目经历")
    public Result<Object> deleteProExpById(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.get("id").toString();
        if(StringUtils.isEmpty(id)){
            return Result.error("id不能为空");
        }
        return Result.ok(proExpService.removeById(id));
    }

    /**
     * 添加/更新教育经历
     */
    @ResponseBody
    @RequestMapping(value = "/updateEduExp", method = RequestMethod.POST)
    @ApiOperation(value = "添加/更新教育经历", notes = "添加/更新教育经历")
    public Result<Object> updateEduExp(@RequestBody WmsEduExp eduExpParam) {
        if (StringUtils.isEmpty(eduExpParam.getId())){
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            eduExpParam.setMemberId(user.getId());
        }
        eduExpService.update(eduExpParam);
        return Result.ok();
    }


    /**
     * 查询教育经历列表
     */
    @ResponseBody
    @RequestMapping(value = "/getEduExpList", method = RequestMethod.GET)
    @ApiOperation(value = "查询教育经历列表", notes = "查询教育经历列表")
    public Result<Object> getEduExpList() {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return Result.OK(eduExpService.list(new QueryWrapper<>(new WmsEduExp().setMemberId(user.getId()))));
    }


    /**
     * 查询教育经历详情
     */
    @ResponseBody
    @RequestMapping(value = "/getEduExpDetail", method = RequestMethod.GET)
    @ApiOperation(value = "查询教育经历详情", notes = "查询教育经历详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query")
    })
    public Result<Object> getEduExpDetail(Long id) {
        return Result.ok(eduExpService.getById(id));
    }

    /**
     * 根据ID删除教育经历
     */
    @ResponseBody
    @RequestMapping(value = "/deleteEduExpById", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID删除教育经历", notes = "根据ID删除教育经历")
    public Result<Object> deleteEduExpById(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.get("id").toString();
        if(StringUtils.isEmpty(id)){
            return Result.error("id不能为空");
        }
        return Result.ok(eduExpService.removeById(id));
    }


    /**
     * 查询简历附件列表
     */
    @ResponseBody
    @RequestMapping(value = "/getResumeFileList", method = RequestMethod.GET)
    @ApiOperation(value = "查询简历附件列表", notes = "查询简历附件列表")
    public Result<Object> getResumeFileList() {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return Result.OK(resumeFileService.list(new QueryWrapper<>(new WmsResumeFile().setMemberId(user.getId()))));
    }

    /**
     * 根据ID删除附件简历
     */
    @ResponseBody
    @RequestMapping(value = "/deleteResumeFileById", method = RequestMethod.GET)
    @ApiOperation(value = "根据ID删除附件简历", notes = "根据ID删除附件简历")
    public Result<Object> deleteResumeFileById(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.get("id").toString();
        if(StringUtils.isEmpty(id)){
            return Result.error("id不能为空");
        }
        return Result.ok(resumeFileService.removeById(id));
    }


    /**
     * 导出简历模板
     */
    @ResponseBody
    @RequestMapping(value = "/exportWord", method = RequestMethod.GET)
    @ApiOperation(value = "导出简历模板", notes = "导出简历模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "简历ID", value = "id", required = true, paramType = "query")
    })
    public Result<Object> exportWord(HttpServletRequest request, HttpServletResponse response, String id) {
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            //判断是否已超过5份简历
            List<WmsResumeFile> list= resumeFileService.list(new QueryWrapper<>(new WmsResumeFile().setId(id).setMemberId(user.getId())));
            if (list.size()==5){
                return Result.error("不能超过5份附件简历哦");
            }
            // 构造freemarker模板引擎参数,listVars.size()个数对应pdf页数
            String url = resumeFileService.exportWordFile(request, new WmsResume().setId(id).setMemberId(user.getId()));
            return Result.ok(url);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("导出失败，请完善简历内容");
        }
    }
}